package com.lhyl.front.web.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lhyl.front.common.SiteUtil;
import com.lhyl.front.constant.ReportConstant;
import com.lhyl.front.constant.UserCoinConstant;
import com.lhyl.front.service.ReportService;
import com.lhyl.front.web.coin.model.UserCoinLog;
import com.lhyl.front.web.coin.service.UserCoinLogService;
import com.lhyl.front.web.shiro.model.User;
import com.lhyl.front.web.shiro.service.UserService;
import com.lhyl.front.web.user.mapper.UserRelationMapper;
import com.lhyl.front.web.user.model.UserCharge;
import com.lhyl.front.web.user.model.UserRelation;
import com.lhyl.front.web.user.service.UserChargeService;
import com.lhyl.front.web.user.service.UserProxyService;

@Service
public class UserProxyServiceImpl implements UserProxyService{

	@Autowired
	private UserRelationMapper userRelationMapper;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ReportService reportService;
	
	@Autowired
	private UserCoinLogService userCoinLogService;
	
	
	
	@Override
	public void createProxy(User user) throws Exception {
		String userId = this.reportService.queryCmsSeq(ReportConstant.USER);
		User superiorProxy = SiteUtil.getLoginUserObject();
		user.setUid(userId);
		userService.createUser(user);//添加用户 
		Date date = new Date();
		UserRelation relation = new UserRelation();
		relation.setIsParent(0);
		relation.setPuid(superiorProxy.getUid());
		relation.setPid(superiorProxy.getId());
		relation.setUid(user.getId());
		relation.setUuid(userId);
		relation.setCreateUser(superiorProxy.getId());
		relation.setModifyUser(superiorProxy.getId());
		relation.setCreateDate(date);
		relation.setModifyDate(date);
		userRelationMapper.insert(relation);
		userRelationMapper.saveParentUserRelation(relation);
	}


	@Override
	public void chargeSubProxy(User user,double coin) throws Exception {
		String coinOrder = this.reportService.queryCmsSeq(ReportConstant.RECHARGE);
		User currentUser =SiteUtil.getLoginUserObject();
		currentUser.setCoin(currentUser.getCoin()-coin);
		user.setCoin(user.getCoin()+coin);
		List<UserCoinLog>coinLogs = new ArrayList<UserCoinLog>();
        UserCoinLog src=  new UserCoinLog();//当前用户的帐变信息
        UserCoinLog target = new UserCoinLog();//目标用户的帐变信息
        src.setCoinOrder(coinOrder);
        src.setCoin(0-coin);
        src.setLiqType(UserCoinConstant.SUB_PROXY_DEDUCTION);
        src.setRemark("为下级会员[“"+user.getUid()+"”]"+UserCoinConstant.SUB_PROXY_DEDUCTION_STR);
        src.setUserCoin(currentUser.getCoin());
        initUserCoinLog(currentUser, currentUser, src);
        initUserCoinLog(user, currentUser, target);
        coinLogs.add(src);
        target.setCoinOrder(coinOrder);
        target.setCoin(coin);
        target.setLiqType(UserCoinConstant.HIGHER_RECHARGE);
        target.setRemark("上级代理[“"+currentUser.getUid()+"”]"+UserCoinConstant.HIGHER_RECHARGE_STR);
        target.setUserCoin(user.getCoin());
        coinLogs.add(target);
        userCoinLogService.addUserCoinLogBatch(coinLogs);
        this.userService.updateUserCoin(currentUser);
        this.userService.updateUserCoin(user);
	}
	
	/***
	 *  初始化用户账单信息
	 * @param target
	 * @param currentUser
	 * @param src
	 */
	private void initUserCoinLog(User target,User currentUser,UserCoinLog src)
	{
		    src.setCoin(target.getCoin());
		    src.setFcoin(target.getFcoin());
	        src.setUid(target.getId());
	        src.setUname(target.getLoginName());
	        src.setUserId(target.getUid());
	        src.setActionUid(currentUser.getId());
	        src.setActionAddr(SiteUtil.getIpAddr());
	        src.setActionDate(new Date());
	}
}
